//
//  DHS2_OrderManagerController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_OrderManagerController.h"
#import "DHS2_OrderCell.h"
#import "DHS2_OrderDetailController.h"

@interface DHS2_OrderManagerController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeftLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewWidthLayout;

@property (strong, nonatomic) NSString *orderType;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *orderArr;

@end

@implementation DHS2_OrderManagerController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"订单管理";
    
    self.orderType = @"全部";
    
    self.lineViewWidthLayout.constant = (kScreenWidth / 5) - 20;
    self.lineViewLeftLayout.constant = 10;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_OrderCell" bundle:nil] forCellReuseIdentifier:kDHS2_OrderCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (NSMutableArray *)orderArr{
    if (!_orderArr) {
        _orderArr = [NSMutableArray array];
    }
    return _orderArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSDictionary *params = [NSDictionary dictionary];
    if ([self.orderType isEqualToString:@"全部"]) {
        params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }else{
        params = @{@"status":self.orderType,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_ordersWithParams:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.orderArr removeAllObjects];
        [self.orderArr addObjectsFromArray:[data objectForKey:@"orders"]];
        [self.tableView reloadData];
        if (self.orderArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
    }];
}

- (void)loadMoreData{
    NSDictionary *params = [NSDictionary dictionary];
    if ([self.orderType isEqualToString:@"全部"]) {
        params = @{@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }else{
        params = @{@"status":self.orderType,@"pageIndex":@(self.pageIndex),@"pageSize":@(20)};
    }
    [[DHNetAPIClient shareDHNetAPIClient] request_ordersWithParams:params andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.orderArr addObjectsFromArray:[data objectForKey:@"orders"]];
        [self.tableView reloadData];
        if (self.orderArr.count < 20 * (self.pageIndex + 1)) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageIndex += 1;
    }];
}

- (IBAction)tabAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    self.lineViewLeftLayout.constant = btn.left + 10;
    
    switch (btn.tag) {
        case 1000:{
            self.orderType = @"全部";
            break;
        }
        case 1001:{
            self.orderType = @"待支付";
            break;
        }
        case 1002:{
            self.orderType = @"待收货";
            break;
        }
        case 1003:{
            self.orderType = @"已完成";
            break;
        }
        case 1004:{
            self.orderType = @"已取消";
            break;
        }
            
        default:
            break;
    }
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 154;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHS2_OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_OrderCell forIndexPath:indexPath];
    NSDictionary *dic = [self.orderArr objectAtIndex:indexPath.row];
    [cell updateCell:dic];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DHS2_OrderDetailController *vc = [[DHS2_OrderDetailController alloc] init];
    NSDictionary *dic = [self.orderArr objectAtIndex:indexPath.row];
    vc.order_id = [dic objectForKey:@"order_id"];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
